﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;


public partial class InventoryTracker_MaterialMaster1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnNew.Enabled = true;
        btnMatDelete.Enabled = false;
        btnMatEdit.Enabled = false;
        btnMatSubmit.Enabled = true;
        btnMatUpdate.Enabled = false;

    }
    protected void btnMatSubmit_Click(object sender, EventArgs e)
    {
        if (txtMatLength.Text == null || txtMatLength.Text == "")
        {
            txtMatLength.Text = "0";
        }
        if (txtMatWidth.Text == null || txtMatWidth.Text == "")
        {
            txtMatWidth.Text = "0";
        }
        if (txtMatHeigth.Text == null || txtMatHeigth.Text == "")
        {
            txtMatHeigth.Text = "0";
        }
        if (txtMatWeight.Text == null || txtMatWeight.Text == "")
        {
            txtMatWeight.Text = "0";
        }

            DataAccess DA = DataAccessHelper.GetDataAccess();
            Material aMaterial = new Material(txtMatItemName.Text,txtMatItemDescription.Text,txtMatItemType.Text,txtMatItemCategory.Text,int.Parse( ddruomID.SelectedValue),float.Parse(txtMatLength.Text),float.Parse( txtMatWidth.Text),float.Parse(txtMatHeigth.Text),float.Parse(txtMatWeight.Text),int.Parse( txtMatReorderQty.Text),
                int.Parse( txtReorderPoint.Text),float.Parse( txtMatSellingRate.Text),int.Parse( ddrLocationID.SelectedValue));
            try
            {
                
                if (DA.CreateMaterial(aMaterial))
                {
                    lblResult.Text = "Success";
                }
                else
                {
                    lblResult.Text = "Failure";
                }
            }
            catch (Exception ex)
            {
                lblResult.Text = ex.Message.ToString();
            }
        }


    protected void DdlItemName_SelectedIndexChanged(object sender, EventArgs e)
    {

        String SelectValue = DdlItemName.SelectedValue;
        ddrLocationID.Visible = false;
        ddruomID.Visible = false;
        txtuomName.Visible = true;
        txtLocName.Visible = true;

        btnNew.Enabled= true;
        btnMatDelete.Enabled = true;
        btnMatEdit.Enabled= true;
        btnMatSubmit.Enabled = false;
        btnMatUpdate.Enabled = false;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM  MaterialMaster WHERE  [ItemCode]=" + SelectValue;
        
        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        txtMatItemName.Text = dataTable.Rows[0]["ItemName"].ToString();
        txtMatItemDescription.Text = dataTable.Rows[0]["ItemDescription"].ToString();
        txtMatItemType.Text = dataTable.Rows[0]["ItemType"].ToString();
        txtMatItemCategory.Text = dataTable.Rows[0]["ItemCategory"].ToString();
        txtMatLength.Text = dataTable.Rows[0]["Lenght"].ToString();
        txtMatWidth.Text = dataTable.Rows[0]["Width"].ToString();
        txtMatHeigth.Text = dataTable.Rows[0]["Height"].ToString();
        txtMatWeight.Text = dataTable.Rows[0]["Weight"].ToString();
        txtMatReorderQty.Text = dataTable.Rows[0]["ReorderQty"].ToString();
        txtReorderPoint.Text = dataTable.Rows[0]["ReorderPoint"].ToString();
        txtMatSellingRate.Text = dataTable.Rows[0]["SellingRate"].ToString();

        String sqlloc = "SELECT * FROM LocationMaster WHERE LocationID=" + dataTable.Rows[0]["LocationID"];
        SqlCommand cmdloc = new SqlCommand(sqlloc, con);
        DataTable dataTableloc = GetDataTable(cmdloc);
        txtLocName.Text = dataTableloc.Rows[0]["LocationName"].ToString();
        
        String sqluom = "SELECT * FROM uom WHERE uomID=" + dataTable.Rows[0]["uomID"];
        SqlCommand cmduom = new SqlCommand(sqluom, con);
        DataTable dataTableuom = GetDataTable(cmduom);
        txtuomName.Text = dataTableuom.Rows[0]["uom"].ToString();

        con.Close();
 
     }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        ddrLocationID.Visible = true;
        ddruomID.Visible = true;
        txtuomName.Visible = false;
        txtLocName.Visible = false;

        btnNew.Enabled = false;
        btnMatDelete.Enabled = false;
        btnMatEdit.Enabled = false;
        btnMatSubmit.Enabled = true;
        btnMatUpdate.Enabled = false;

        txtMatItemName.Text = "";
        txtMatItemDescription.Text = "";
        txtMatItemType.Text = "";
        txtMatItemCategory.Text = "";
        txtMatLength.Text = "";
        txtMatWidth.Text = "";
        txtMatHeigth.Text = "";
        txtMatWeight.Text = "";
        txtMatReorderQty.Text = "";
        txtReorderPoint.Text = "";
        txtMatSellingRate.Text = "";
        ddrLocationID.SelectedIndex=0;
        ddruomID.SelectedIndex=0;
    }
    
    protected void btnMatDelete_Click(object sender, EventArgs e)
    {
        String SelectValue = DdlItemName.SelectedValue;
        btnNew.Enabled = false;
        btnMatDelete.Enabled=true;
        btnMatEdit.Enabled = false;
        btnMatSubmit.Enabled = false;
        btnMatUpdate.Enabled = false;


        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM MaterialMaster WHERE  [ItemCode]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtMatItemName.Text = "";
        txtMatItemDescription.Text = "";
        txtMatItemType.Text = "";
        txtMatItemCategory.Text = "";
        txtMatLength.Text = "";
        txtMatWidth.Text = "";
        txtMatHeigth.Text = "";
        txtMatWeight.Text = "";
        txtMatReorderQty.Text = "";
        txtReorderPoint.Text = "";
        txtMatSellingRate.Text = "";
        ddrLocationID.SelectedIndex = 0;
        ddruomID.SelectedIndex = 0;

    }
    protected void btnMatEdit_Click(object sender, EventArgs e)
    {
        ddrLocationID.Visible = true;
        ddruomID.Visible = true;
        txtuomName.Visible = false;
        txtLocName.Visible = false;

        btnNew.Enabled = false;
        btnMatDelete.Enabled = false;
        btnMatEdit.Enabled = false;
        btnMatSubmit.Enabled = false;
        btnMatUpdate.Enabled = true;


        
        
    }
    protected void btnMatUpdate_Click(object sender, EventArgs e)
    {
        btnNew.Enabled = false;
        btnMatDelete.Enabled = false;
        btnMatEdit.Enabled = false;
        btnMatSubmit.Enabled = false;
        btnMatUpdate.Enabled = true;


        String SelectValue = DdlItemName.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE MaterialMaster SET  ItemName = '" + txtMatItemName.Text + "' , ItemDescription = '" + txtMatItemDescription.Text + "' , ItemType ='" + txtMatItemType.Text + "', ItemCategory ='" + txtMatItemCategory.Text + "' , Lenght ='" + txtMatLength.Text + "' , Width ='" + txtMatWidth.Text + "', Height = '" + txtMatHeigth.Text + "', Weight = '" + txtMatWeight.Text + "', ReorderQty= '" + txtMatReorderQty.Text + "',ReorderPoint='" + txtReorderPoint.Text + "',SellingRate='" + txtMatSellingRate.Text + "' WHERE ItemCode=" + SelectValue;
        //String sql= "UPDATE LocationMaster SET LocationName = @LocationName, LocAddr1 = @LocAddr1, LocAddr2 = @LocAddr2, LocCity = @LocCity, LocState = @LocState, LocCountry = @LocCountry, LocPhone = @LocPhone, LocFax = @LocFax, Remarks = @Remarks WHERE [LocationID] = " + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

    }
}

